/**
 * Message.java
 * 
 * @version 1.0
 *
 * Mar 29, 2012
 * 
 * Licensed to Open-Ones Group under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Open-Ones Group licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 * Modification Logs:
 *   DATE        AUTHOR        DESCRIPTION
 *  ------------------------------------------------------------
 *  7:53:49 PM  DanhDTC.      Initial.
 *
 */
package vn.edu.hcmut.mmt.data;

import vn.edu.hcmut.mmt.error.MMTException;

/**
 * This class is used for Message.
 * @author DanhDTC
 *
 */
public class Message {
    private int sender;
    private int receiver;
    private String message;
    
    /**
     * Construct new Message.
     *
     */
    public Message() {
        super();
    }
    /**
     * Construct new Message.
     *
     * @param encodedMessage Full message receive from server
     *          or other clients. This message is generated by {@link Message#toString()}.
     * @throws MMTException Exception when msg give not correct format.
     */
    public Message(String encodedMessage) throws MMTException
    {
       
        // Check if this is a message.
        if (!encodedMessage.startsWith("Message "))
        {
            throw new MMTException("Input isn't a message.");
        }
        
        // find sender value.
        int index = encodedMessage.indexOf("sender=");
        if (index < 0)
        {
            throw new MMTException("Sender not found.");
        }
        String tmp = encodedMessage.substring(index + "sender=".length());
        String number = tmp.substring(0,tmp.indexOf(", ")+1);
        if (number.matches("\\d+$"))
        {
            sender = Integer.parseInt(number);
        }
        else
        {
            throw new MMTException("Sender's ID isn't correct format.");
        }
        
        // find receiver value.
         index = encodedMessage.indexOf(", receiver=");
        if (index < 0)
        {
            throw new MMTException("Sender not found.");
        }
         tmp = encodedMessage.substring(index + ", receiver=".length());
         number = tmp.substring(0,tmp.indexOf(", ")+1);
        if (number.matches("\\d+$"))
        {
            sender = Integer.parseInt(number);
        }
        else
        {
            throw new MMTException("Receiver's ID isn't correct format.");
        }
        
        // find message value
        index = encodedMessage.indexOf(", message=");
        if (index < 0)
        {
            throw new MMTException("Message not found.");
        }
         tmp = encodedMessage.substring(index + ", message=".length());
         message = tmp.substring(0, tmp.length() - 1);
    }
    /**
     * Construct new Message.
     *
     * @param sender ID of the sender.
     * @param receiver ID of the receiver.
     * @param message message.
     */
    public Message(int sender, int receiver, String message) {
        super();
        this.sender = sender;
        this.receiver = receiver;
        this.message = message;
    }
    /**
    * Get value of sender.
    * @return the sender
    */
    public int getSender() {
        return sender;
    }
    /**
     * Set the value for sender.
     * @param sender the sender to set
     */
    public void setSender(int sender) {
        this.sender = sender;
    }
    /**
    * Get value of receiver.
    * @return the receiver
    */
    public int getReceiver() {
        return receiver;
    }
    /**
     * Set the value for receiver.
     * @param receiver the receiver to set
     */
    public void setReceiver(int receiver) {
        this.receiver = receiver;
    }
    /**
    * Get value of message.
    * @return the message
    */
    public String getMessage() {
        return message;
    }
    /**
     * Set the value for message.
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }
    /**
     * [Explain the description for this method here].
     * @return message will be sent.
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Message [sender=");
        builder.append(sender);
        builder.append(", receiver=");
        builder.append(receiver);
        builder.append(", message=");
        builder.append(message);
        builder.append("]");
        return builder.toString();
    }

}
